package com.chaos.auth.config.userdetails.details;

import com.chaos.auth.api.enums.UserStatusEnums;
import com.chaos.system.api.data.TUserData;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;

import java.time.LocalDate;
import java.util.Collection;

/**
 * 适用于C 端 用户
 */
@NoArgsConstructor
@Data
public class AppUserDetails implements UserDetails {
    private Long userId;

    private String username;

    private Boolean enabled;

    private Long tenantId;

    @ApiModelProperty(value = "头像")
    private String avatar;

    @ApiModelProperty(value = "生日")
    private LocalDate birthday;

    @ApiModelProperty(value = "性别 保密、男、女 对应0、1 、2")
    private Byte gender;

    @ApiModelProperty(value = "phone")
    private String phone;
    private Long userInfoId;

    private String password;
    private Boolean accountNonLocked;
    private Boolean accountNonExpired;
    private Boolean credentialsNonExpired;

    /**
     * 用户信息构建
     *
     * @param userDto 用户参数信息
     */
    public AppUserDetails(TUserData userDto) {
        this.setUserId(userDto.getId());
        //不可以为空
        this.setUsername(String.valueOf(userDto.getId()));
        this.setEnabled(userDto.getStatus().equals(UserStatusEnums.IN_USE.getCode()));
        this.setTenantId(userDto.getTenantId());
        this.setAvatar(userDto.getAvatar());
        this.setBirthday(userDto.getBirthday());
        this.setGender(userDto.getGender());
        this.setPhone(userDto.getPhone());
        this.setUserInfoId(userDto.getUserInfoId());
    }

    @Override
    public Collection<? extends GrantedAuthority> getAuthorities() {
        return null;
    }

    @Override
    public String getPassword() {
        return null;
    }

    @Override
    public String getUsername() {
        return this.username;
    }

    @Override
    public boolean isAccountNonExpired() {
        return false;
    }

    @Override
    public boolean isAccountNonLocked() {
        return false;
    }

    @Override
    public boolean isCredentialsNonExpired() {
        return false;
    }

    @Override
    public boolean isEnabled() {
        return this.enabled;
    }
}
